import 'package:bot_toast/bot_toast.dart';
import 'package:crop_image_project/loading_widget.dart';
import 'package:flutter/material.dart';

/// Since: 2024/9/5
/// Author: Elvis.Zhao
/// Version: v1.0
/// Description:

class LoadingViewUtils {
  factory LoadingViewUtils() => getInstance();

  static LoadingViewUtils? _instance;

  LoadingViewUtils._();

  static LoadingViewUtils getInstance() {
    if (_instance == null) {
      _instance = LoadingViewUtils._();
    }
    return _instance!;
  }

  CancelFunc? _cancelFunc;
  bool _showing = false;

  _showLoadingView(bool clickClose, bool allowClick, BackButtonBehavior backButtonBehavior, bool ignoreContentClick, {String? text = "Loading"}) {
    if (_cancelFunc == null) {
      _cancelFunc = BotToast.showCustomLoading(
          clickClose: clickClose,
          allowClick: allowClick,
          backButtonBehavior: backButtonBehavior,
          ignoreContentClick: ignoreContentClick,
          animationDuration: Duration(milliseconds: 300),
          animationReverseDuration: Duration(milliseconds: 300),
          backgroundColor: Color(0x42000000),
          align: Alignment.center,
          toastBuilder: (cancelFunc) {
            _showing = true;
            return CustomLoadWidget(loadText: text, cancelFunc: cancelFunc);
          });
    } else {
      if (!_showing) {
        _cancelFunc = BotToast.showCustomLoading(
            clickClose: clickClose,
            allowClick: allowClick,
            backButtonBehavior: backButtonBehavior,
            ignoreContentClick: ignoreContentClick,
            animationDuration: Duration(milliseconds: 300),
            animationReverseDuration: Duration(milliseconds: 300),
            backgroundColor: Color(0x42000000),
            align: Alignment.center,
            toastBuilder: (cancelFunc) {
              _showing = true;
              return CustomLoadWidget(loadText: text, cancelFunc: cancelFunc);
            });
      }
    }
  }

  ///不可点击，不可取消，拦截返回键的loadingView
  showLoadingView({String? text = "Loading", bool clickClose = true, bool ignoreContentClick = false}) {
    if (TextUtils.isEmpty(text)) {
      text = "Loading";
    }
    _showLoadingView(clickClose, true, BackButtonBehavior.ignore, ignoreContentClick, text: text);
  }

  ///隐藏loadingView
  dismissLoadingView() {
    if (_showing) {
      _cancelFunc?.call();
      _cancelFunc = null;
    }
  }
}

class TextUtils {
  ///判断当前字符串是否为空
  static bool isEmpty(String? content) {
    if (content != null && content.isNotEmpty) {
      return false;
    }
    return true;
  }

  static bool isNotEmpty(String? content) {
    if (content != null && content.isNotEmpty) {
      return true;
    }
    return false;
  }
}
